import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:myflutterproject/routes.dart';
import 'package:myflutterproject/utils/overScrollBehavior.dart';
import 'package:myflutterproject/utils/toast.dart';

class MenuPage extends StatelessWidget {
  DateTime? _lassPressedAt;
  @override
  Widget build(BuildContext context) {
    /**
     * WillPopScope用于处理是否离开当前页面
     * DateTime.now().difference()计算差值
     */
    return WillPopScope(
      onWillPop: () async {
        if (_lassPressedAt == null ||
            DateTime.now().difference(_lassPressedAt!) > Duration(seconds: 2)) {
          Toast.toast(context, msg: '再次点击退出');
          print('再次点击退出');
          _lassPressedAt = DateTime.now();
          return false;
        } else {
          /*退出app*/
          // Navigator.of(context).pop(true);
          await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          print('退出');
          return true;
        }
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text('各种学习示例'),
          centerTitle: true,
        ),
        body: Center(
          child: ScrollConfiguration(
            behavior: OverScrollBehavior(),
            child: ListView(
              children: <Widget>[
                ItemButton('零碎知识点整理', homePage),
                ItemButton('Flutter中文网', flutterdemo),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class ItemButton extends StatelessWidget {
  final String name;
  final String route;

  ItemButton(this.name, this.route);

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      child: Text(
        name,
        style: TextStyle(fontSize: 20, color: Colors.blue),
      ),
      onPressed: () => {Navigator.pushNamed(context, route)},
    );
  }
}
